package com.qs.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.qs.annotation.OperationLog;
import com.qs.pojo.vo.OnlineVo;
import com.qs.response.Response;
import com.qs.service.IAsyncService;
import com.qs.service.IOnlineService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 用户在线状态表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-01-06
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/online")
@RequiredArgsConstructor
public class OnlineController {

    private final IOnlineService onlineService;

    private final IAsyncService asyncService;

    /**
     * 分页获取用户是否在线数据
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/list/{pageNO}/{pageSize}")
    public Response list(@PathVariable("pageNO") Integer pageNo,
                         @PathVariable("pageSize") Integer pageSize,
                         OnlineVo onlineVo){
        log.info("分页获取用户是否在线数据请求进来了-----------");
        Map<String,Object> map = onlineService.getOnlineList(pageNo, pageSize,onlineVo);
        return Response.success(map);
    }

    /**
     * 管理员踢人下线
     * @param userId
     * @return
     */
    @GetMapping("/offline/{userId}")
    @OperationLog(operModul = "用户在线管理-踢下线",operDesc = "管理员踢下线用户")
    public Response offline(@PathVariable("userId") Integer userId){
        log.info("管理员踢人下线请求进来了-----------");
        asyncService.changeUserOfflineStatus(userId);
        //踢下线
        StpUtil.kickout(userId);
        return Response.success();
    }
}
